<?php
namespace Qwadmin\Controller;

use Vendor\Page;

class OrderController extends ComController
{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 消费/订单 记录
	 * */
	public function index(){
		$p = $_GET["p"];
		$p = intval($p) > 0 ? $p : 1;
		
		//订单状态
		//暂时只查询已付费的
		$map["status"] = "1";
		if(!empty($_GET["bdate"])){
			$map["paytime"] = array("EGT",$_GET["bdate"]." 00:00:00");
			$this->assign("bdate", $_GET["bdate"]);
		}
		if(!empty($_GET["edate"])) {
			$map["paytime"] = array("ELT",$_GET["edate"]." 23:59:59");
			$this->assign("edate", $_GET["edate"]);
		}
		if(!empty($_GET["bdate"]) && !empty($_GET["edate"])){
			$map["paytime"] = array('between',$_GET["bdate"]." 00:00:00,".$_GET["edate"]." 23:59:59");			
		}		
		
		
		$class = M('order');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
		
		$count = $class->where($map)->count();
		//$list = $class->where($map)->order("order_id desc")->limit($offset . ',' . $pagesize)->select();
		$list = $class->join("qw_user ON qw_order.open_id = qw_user.open_id")->field("qw_user.addtime as u_addtime,money,qw_order.addtime as addtime,paytime,nickname")->order("order_id desc")->limit($offset . ',' . $pagesize)->where($map)->select();
		//echo $class->getLastSql();die;
		//print_r($list);die;
		
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		$this->display();
	}
	/**
	 * 订单统计
	 * */
	public function count(){
		$order = M("order");
		//当日统计
		$daynow=date("Y-m-d");
		$where["paytime"]=array("like",$daynow."%");//完成支付的时间
		$where["addtime"]=array("like",$daynow."%");//用户进入阅读的时间
		$where["status"]=1;
		$result["status"]=0;
		$result["addtime"]=array("like",$daynow."%");//未完成支付的用户进入阅读的时间
		$data["already_paid"] = $order->where($where)->count();//当前时间充值的用户数量		
		$data["total"] = $order->where($where)->sum("money")/100;//当前时间充值金额总数		
		$data["unpaid"]=$order->where($result)->count();//当前时间未充值的用户数量		
		$result["rate"]=$data["already_paid"]/($data["already_paid"]+$data["unpaid"]);
		$data["rate"]=floor($result["rate"]*10000)/10000*100;//当前的完成率
		$this->assign("pays_day",$data);
		$this->assign("Today",$daynow);
		//昨日统计
		$todaynow=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
		$where_today["paytime"]=array("like",$todaynow."%");
		$where_today["addtime"]=array("like",$todaynow."%");
		$where_today["status"]=1;
		$consequence["status"]=0;
		$consequence["addtime"]=array("like",$todaynow."%");
		$finish["already_paid"]= $order->where($where_today)->count();//昨天充值的用户数量			
		$finish["total"] = $order->where($where_today)->sum("money")/100;//昨天充值金额总数		
		$finish["unpaid"]=$order->where($consequence)->count();//昨天未充值的用户数量
		$consequence["rate"]=$finish["already_paid"]/($finish["already_paid"]+$finish["unpaid"]);
		$finish["rate"]=floor($consequence["rate"]*10000)/10000*100;//昨天的完成率
		$this->assign("pays_today",$finish);
		$this->assign("Yesterday",$todaynow);
		//前天统计
		$before_yesterday=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-2,date("Y")));
		$where_before_yesterday["paytime"]=array("like",$before_yesterday."%");
		$where_before_yesterday["addtime"]=array("like",$before_yesterday."%");
		$where_before_yesterday["status"]=1;
		$consequence_before_yesterday["status"]=0;
		$consequence_before_yesterday["addtime"]=array("like",$before_yesterday."%");
		$finish_before_yesterday["already_paid"]= $order->where($where_before_yesterday)->count();//前天充值的用户数量
		$finish_before_yesterday["total"] = $order->where($where_before_yesterday)->sum("money")/100;//前天充值金额总数
		$finish_before_yesterday["unpaid"]=$order->where($consequence_before_yesterday)->count();//前天未充值的用户数量
		$consequence_before_yesterday["rate"]=$finish_before_yesterday["already_paid"]/($finish_before_yesterday["already_paid"]+$finish_before_yesterday["unpaid"]);		
		$finish_before_yesterday["rate"]=floor($consequence_before_yesterday["rate"]*10000)/10000*100;//前天的完成率
		$this->assign("pays_before_yesterday",$finish_before_yesterday);
		$this->assign("before_yesterday",$before_yesterday);
		//大前天统计
		$days_ago=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-3,date("Y")));
		$where_days_ago["paytime"]=array("like",$days_ago."%");
		$where_days_ago["addtime"]=array("like",$days_ago."%");
		$where_days_ago["status"]=1;
		$consequence_days_ago["status"]=0;
		$consequence_days_ago["addtime"]=array("like",$days_ago."%");
		$finish_days_ago["already_paid"]= $order->where($where_days_ago)->count();//大前天充值的用户数量
		$finish_days_ago["total"] = $order->where($where_days_ago)->sum("money")/100;//大前天充值金额总数
		$finish_days_ago["unpaid"]=$order->where($consequence_days_ago)->count();//大前天未充值的用户数量
		$consequence_days_ago["rate"]=$finish_days_ago["already_paid"]/($finish_days_ago["already_paid"]+$finish_days_ago["unpaid"]);
		$finish_days_ago["rate"]=floor($consequence_days_ago["rate"]*10000)/10000*100;//大前天的完成率
		$this->assign("pays_days_ago",$finish_days_ago);
		$this->assign("days_ago",$days_ago);
		//当月统计
		$monthnow=date("Y-m");
		$where_month["paytime"]=array("like",$monthnow."%");
		$where_month["addtime"]=array("like",$monthnow."%");
		$where_month["status"]=1;
		$outcome["status"]=0;
		$outcome["addtime"]=array("like",$monthnow."%");
		$finally["already_paid"]= $order->where($where_month)->count();//当月时间充值的用户数量		
		$finally["total"] = $order->where($where_month)->sum("money")/100;//当月时间充值金额总数		
		$finally["unpaid"]=$order->where($outcome)->count();//当月时间未充值的用户数量		
		$outcome["rate"]=$finally["already_paid"]/($finally["already_paid"]+$finally["unpaid"]);
		$finally["rate"]=floor($outcome["rate"]*10000)/10000*100;//当月的完成率
		$this->assign("pays_month",$finally);
		//累计统计
		$Last["status"]=1;
		$ultimate["status"]=0;
		$Last["already_paid"]= $order->where($Last)->count();//总共充值的用户数量
		$Last["total"] = $order->where($Last)->sum("money")/100;//总共充值金额总数
		$Last["unpaid"]=$order->where($ultimate)->count();//总共未充值的用户数量
		$ultimate["rate"]=$Last["already_paid"]/($Last["already_paid"]+$Last["unpaid"]);
		$Last["rate"]=floor($ultimate["rate"]*10000)/10000*100;//总共的完成率
		$this->assign("pays_altogether",$Last);
		$this->display();
		//dump($data);
	}
	/**
	 * 用户统计
	 * */
	public function statistics(){
		$user=M(user);
		//今日统计
		$daynow=date("Y-m-d");
		$where_man["addtime"]=array("like",$daynow."%");
		$where_man["sex"]=1;
		$where_woman["sex"]=0;
		$where_woman["addtime"]=array("like",$daynow."%");
		$data_day["man"]=$user->where($where_man)->count();//当日新增男性用户
		$data_day["woman"]=$user->where($where_woman)->count();//当日新增女性用户
		$data_day["total"]=$data_day["man"]+$data_day["woman"];//当日新增用户总量
		$this->assign("added_day",$data_day);
		$this->assign("Today",$daynow);
		//昨日统计
		$todaynow=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
		$where_man_today["addtime"]=array("like",$todaynow."%");
		$where_man_today["sex"]=1;
		$where_woman_today["sex"]=0;
		var_dump($where_woman_today["sex"];
		$where_woman_today["addtime"]=array("like",$todaynow."%");
		$data_today["man"]=$user->where($where_man_today)->count();//昨日新增男性用户
		$data_today["woman"]=$user->where($where_woman_today)->count();//昨日新增女性用户
		$data_today["total"]=$data_today["man"]+$data_today["woman"];//昨日新增用户总量	
		$this->assign("added_today",$data_today);
		$this->assign("Todaynow",$todaynow);
		//前天统计
		$before_yesterdaynow=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-2,date("Y")));
		$where_man_byesterday["addtime"]=array("like",$before_yesterdaynow."%");
		$where_man_byesterday["sex"]=1;
		$where_woman_byesterday["sex"]=0;
		$where_woman_byesterday["addtime"]=array("like",$before_yesterdaynow."%");
		$data_byesterday["man"]=$user->where($where_man_byesterday)->count();//前天新增男性用户
		$data_byesterday["woman"]=$user->where($where_woman_byesterday)->count();//前天新增女性用户
		$data_byesterday["total"]=$data_byesterday["man"]+$data_byesterday["woman"];//前天新增用户总量
		$this->assign("byesterday",$data_byesterday);
		$this->assign("before_yesterdaynow",$before_yesterdaynow);
		//大前天统计
		$days_ago=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-3,date("Y")));
		$where_man_days_ago["addtime"]=array("like",$days_ago."%");
		$where_man_days_ago["sex"]=1;
		$where_woman_days_ago["sex"]=0;
		$where_woman_days_ago["addtime"]=array("like",$days_ago."%");	
		$data_days_ago["man"]=$user->where($where_man_days_ago)->count();//大前天新增男性用户
		$data_days_ago["woman"]=$user->where($where_woman_days_ago)->count();//大前天新增女性用户	
		$data_days_ago["total"]=$data_days_ago["man"]+$data_days_ago["woman"];//大前天新增用户总量	
		$this->assign("user_days_ago",$data_days_ago);
		$this->assign("days_agonow",$days_ago);
		//大大前天统计
		$days_greatly=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-4,date("Y")));
		$where_man_days_greatly["addtime"]=array("like",$days_greatly."%");
		$where_man_days_greatly["sex"]=1;
		$where_woman_days_greatly["sex"]=0;
		$where_woman_days_greatly["addtime"]=array("like",$days_greatly."%");
		$data_days_greatly["man"]=$user->where($where_man_days_greatly)->count();//大大前天新增男性用户
		$data_days_greatly["woman"]=$user->where($where_woman_days_greatly)->count();//大大前天新增女性用户
		$data_days_greatly["total"]=$data_days_greatly["man"]+$data_days_greatly["woman"];//大大前天新增用户总量
		$this->assign("user_days_greatly",$data_days_greatly);
		$this->assign("days_greatly",$days_greatly);
		//当月统计
		$daymonth=date("Y-m");
		$where_man_month["addtime"]=array("like",$daymonth."%");
		$where_man_month["sex"]=1;
		$where_woman_today["sex"]=0;
		$where_woman_today["addtime"]=array("like",$daymonth."%");
		$data_month["man"]=$user->where($where_man_month)->count();//当月新增男性用户
		$data_month["woman"]=$user->where($where_woman_today)->count();//当月新增女性用户	
		$data_month["total"]=$data_month["man"]+$data_month["woman"];//当月新增用户总量
		$this->assign("added_month",$data_month);
		//累计统计
		$where_man_overall["sex"]=1;
		$where_woman_overall["sex"]=0;
		$data_overall["man"]=$user->where($where_man_overall)->count();//总共新增男性用户
		$data_overall["woman"]=$user->where($where_woman_overall)->count();//总共新增女性用户
		$data_overall["total"]=$data_overall["man"]+$data_overall["woman"];//总共新增用户总量	
		$this->assign("added_overall",$data_overall);
		
		$this->display();
	}
	
	
}